package com.ebeitech.library.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by alice on 2018/5/30.
 */

public class ImageUtil {

	public final static int IMAGE_WIDTH_MAX = 480;
	public final static int IMAGE_HEIGHT_MAX = 800;

	/**
	 * 把bitmap转为Drawable
	 * @param bitmap
	 * @return
	 */
	public static Drawable convertBitmapToDrawable(Context context, Bitmap bitmap) {
		BitmapDrawable bd = new BitmapDrawable(context.getResources(), bitmap);
		return bd;
	}

	/**
	 * 把字节转为bitmap
	 * @param data
	 * @return
	 */
	public static Bitmap convertBytesToBimap(byte[] data) {
		if (data.length != 0) {
			return BitmapFactory.decodeByteArray(data, 0, data.length);
		} else {
			return null;
		}
	}

	/**
	 * Drawable转换成InputStream
	 * @param drawable
	 * @return
	 */
	public static InputStream convertDrawableToInputStream(Drawable drawable) {
		Bitmap bitmap = convertDrawableToBitmap(drawable);
		return convertBitmapToInputStream(bitmap);
	}

	/**
	 * Drawable转换成Bitmap
	 * @param drawable
	 * @return
	 */
	public static Bitmap convertDrawableToBitmap(Drawable drawable) {
		BitmapDrawable bd = (BitmapDrawable)drawable;
		return bd.getBitmap();
	}

	/**
	 * 将Bitmap转换成InputStream
	 * @param bm
	 * @return
	 */
	public static InputStream convertBitmapToInputStream(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		InputStream is = new ByteArrayInputStream(baos.toByteArray());
		return is;
	}

	/**
	 * 计算原图片到目标图片的缩放比例就可以计算出图片的压缩比
	 * @param options
	 * @param targetWidth
	 * @param targetHeight
	 * @return
	 */
	public static int calculateInSampleSize(BitmapFactory.Options options, int targetWidth, int targetHeight) {
		int picWidth = options.outWidth;
		int picHeight = options.outHeight;
		if(targetWidth <= 0 && targetHeight <= 0) {
			return 1;
		}
		if(targetWidth <= 0) {
			return (int) Math.ceil(picHeight / targetHeight);
		}
		if(targetHeight <= 0) {
			return (int) Math.ceil(picWidth / targetWidth);
		}

		int inSampleSize = 1;
		float scaleX = picWidth / targetWidth;
		float scaleY = picHeight / targetHeight;
		if (scaleX > 1 || scaleY > 1) {
			// 图片超出目标大小，需要缩小
			if (scaleX > scaleY) {
				// 横向比例缩小
				inSampleSize = (int) Math.ceil(scaleX);
			} else {
				// 纵向比例缩小
				inSampleSize = (int) Math.ceil(scaleY);
			}
		}
		return inSampleSize;
	}

	/**
	 * 计算原图片到目标图片的缩放比例就可以计算出图片的压缩比
	 * @param bitmap
	 * @param targetWidth
	 * @param targetHeight
	 * @return
	 */
	public static float calculateScaleSize(Bitmap bitmap, float targetWidth, float targetHeight) {
		int picWidth = bitmap.getWidth();
		int picHeight = bitmap.getHeight();
		if(targetWidth <= 0 || targetHeight <= 0 || picWidth <= 0 || picHeight <= 0) {
			return 1;
		}
		float scaleWidth = targetWidth / picWidth;
		float scaleHeight = targetHeight / picHeight;
		if(scaleWidth > scaleHeight) {
			return scaleHeight;
		}
		return scaleWidth;
	}

	/**
	 * 释放Bitmap
	 * @param bitmap
	 */
	public static void releaseBitmap(Bitmap bitmap) {
		if(bitmap != null) {
			bitmap.recycle();
			bitmap = null;
		}
		System.gc();
	}

	public static void doCompress(String fromFile, String toFile, int width, int height) {
		try {
			Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
			int bitmapWidth = bitmap.getWidth();
			int bitmapHeight = bitmap.getHeight();
			// 缩放图片的尺寸，以长边为缩放参照
			// 例如，400*300的图片，缩放到100，那么缩放倍数就是100/400=0.25，最终图片分辨率为100*75；
			// 300*400的图片，缩放到200，那么缩放倍数就是200/400=0.5，最终图片分辨率为150*200。
			float scale = 1;
			if (bitmapWidth > bitmapHeight) {
				scale = (float) width / bitmapWidth;
			} else {
				scale = (float) height / bitmapHeight;
			}
			Matrix matrix = new Matrix();
			matrix.postScale(scale, scale);
			// 产生缩放后的Bitmap对象
			Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0,
					bitmapWidth, bitmapHeight, matrix, false);
			// save file
			File myCaptureFile = new File(toFile);
			FileOutputStream out = new FileOutputStream(myCaptureFile);
			if (resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
				out.flush();
				out.close();
			}
			if (!bitmap.isRecycled()) {
				bitmap.recycle();// 记得释放资源，否则会内存溢出
			}
			if (!resizeBitmap.isRecycled()) {
				resizeBitmap.recycle();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

}
